﻿<?php
	 // 不多说了，直接上转载请著名出处 php100.com
	include "http.class.php";
	include 'ChromePhp.php';
	class qq {
	 
	     public $sid;
	     public $http;
	     public $qq_num;
 
	     function __construct() {
	         $this->http = new http;
	     }
	 
	     function login($qq_num, $qq_pwd) {
             $data = $this->http->get("http://pt.3g.qq.com/");
			 
			 /*
			 int preg_match( string pattern, string subject [, array matches [, int flags]] )
				在 subject 字符串中搜索与pattern给出的正则表达式相匹配的内容。
				如果提供了 matches，则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本，$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本，以此类推。
			 */
	         $action = preg_match("/action=\"(.+)?\"/", $data, $matches);
	         $action = $matches[1];
	         $params = array();
	         $params["login_url"] = "http://pt.3g.qq.com/s?aid=nLogin";
	         $params["sidtype"] = 1;
	         $params["loginTitle"] = "手机腾讯网";
	         $params["bid"] = 0;
	         $params["qq"] = $qq_num;
	         $params["pwd"] = $qq_pwd;
	         $params["loginType"] =1;
			 /*
			 string http_build_query[1] ( array formdata [, string numeric_prefix])
			 根据数组产生一个urlencode之后的请求字符串，如果在基础数组中使用了数字下标同时给出了 numeric_prefix参数。
			 */
	         $data = $this->http->post($action, http_build_query($params));
	         if(count(explode("验证码",$data))>1){//explode()分割字符串//count() 函数计算数组中的单元数目或对象中的属性个数。
	              preg_match("/<img src=\"(.+?)\"/", $data, $matches);
	              exit("需要输入验证码");
	         }
	         $action = preg_match("/sid=(.+?)&/", $data, $matches);
	         $this->sid = $matches[1];
	         return $this->sid;
	     }
	 
	     function sendMsg($to_num, $msg, $sid = 0) {
	         $sid = $sid ? $sid : $this->sid;
	         if (!$sid)
	             exit("sid值未传入进去");
	         $params = array();
	         $params["msg"] = $msg;
	         $params["u"] = $to_num;
	         $params["saveURL"] = 0;
	         $params["do"] = "send";
	         $params["on"] = 1;
	         $params["aid"] = "sendmsg";
			 $params["tfor"] = "qq";
	         $url = "http://q16.3g.qq.com/g/s?sid=" . $sid;
	         $data = $this->http->post($url, http_build_query($params));
	         return $data;
	     }
	 
	     function getMsg($qq_num = 0, $sid = 0) {
	         $qq_num = $qq_num ? $qq_num : $this->qq_num;
	         if (!$qq_num)
	             exit("qq_num值未传入进去");
	         $sid = $sid ? $sid : $this->sid;
	         if (!$sid)
	             exit("sid值未传入进去");
	         $url = "http://q16.3g.qq.com/g/s?sid=" . $sid . "&3G_UIN=" . $qq_num ."&saveURL=0&aid=nqqChat";
             $data = $this->http->get($url);
	         preg_match("/name=\"u\" value=\"(\d+)\"/", $data, $matches);
	         $result["qq"] = $matches[1];
	         $data = explode("<form", $data);
	         $data = $data[0];
	         preg_match_all("/<p>(.+)?<\/p>/", $data, $matches);
	         unset($matches[1][0]);
	         $result["content"] = $matches[1];
	         return $result;
	     }
	     function logout($sid){
	         $url="http://pt.3g.qq.com/s?sid=".$sid."&aid=nLogout";
	          
	         echo $this->http->get($url);
	     }
	}
?>